import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_syt/datas/user_info.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/theme/font_styles.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:table_calendar/table_calendar.dart';
import 'package:yyy_route_annotation/yyy_route_annotation.dart';

import 'package:flutter_syt/domain/widgets/person_info_card.dart';
import 'package:flutter_syt/domain/widgets/stupid_title_bar.dart';

final kToday = DateTime.now();

final kFirstDay = DateTime(kToday.year, kToday.month - 3, kToday.day);
final kLastDay = DateTime(kToday.year, kToday.month + 3, kToday.day);

// 签到
@RoutePage(name: "pointSignInPage")
class PointSignInPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _PointSignInPageState();
}

class _PointSignInPageState extends State<PointSignInPage> {
  // 用户基本信息
  // PersonInfoEntity userInfo = PersonInfoEntity(
  //   '刘亦菲',
  //   '200',
  //   'https://pic2.zhimg.com/v2-639b49f2f6578eabddc458b84eb3c6a1.jpg',
  //   user.roles,
  //   '铂金',
  //   '8',
  //   '1.5',
  // );
  CalendarFormat _calendarFormat = CalendarFormat.month;
  DateTime _focusedDay = DateTime.now();
  DateTime? _selectedDay;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          S.of(context).sign_in,
        ),
      ),
      body: Container(
        height: double.infinity,
        child: Container(
          width: MediaQuery.of(context).size.width,
          height: 600,
          child: ListView(
            physics: BouncingScrollPhysics(),
            children: [
              // _personInfo(),
              _signInCalendar(),
              SizedBox(
                height: 30,
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _personInfo(PersonInfoEntity userInfo) {
    return PersonInfoCard(
      userInfo,
      rightButtonText: '已签到',
    );
  }

  Widget _tableCalendar() {
    return TableCalendar(
      firstDay: kFirstDay,
      lastDay: kLastDay,
      focusedDay: _focusedDay,
      calendarFormat: _calendarFormat,
      headerVisible: false,
      selectedDayPredicate: (day) {
        // Use `selectedDayPredicate` to determine which day is currently selected.
        // If this returns true, then `day` will be marked as selected.

        // Using `isSameDay` is recommended to disregard
        // the time-part of compared DateTime objects.
        return isSameDay(_selectedDay, day);
      },
      onDaySelected: (selectedDay, focusedDay) {
        if (!isSameDay(_selectedDay, selectedDay)) {
          // Call `setState()` when updating the selected day
          setState(() {
            _selectedDay = selectedDay;
            _focusedDay = focusedDay;
          });
        }
      },
      onFormatChanged: (format) {
        if (_calendarFormat != format) {
          // Call `setState()` when updating calendar format
          setState(() {
            _calendarFormat = format;
          });
        }
      },
      onPageChanged: (focusedDay) {
        // No need to call `setState()` here
        _focusedDay = focusedDay;
      },
      calendarBuilders: CalendarBuilders(
        todayBuilder: (context, day, focusedDay) {
          final text = '${day.day}';
          final calendarStyle = new CalendarStyle();
          final margin = calendarStyle.cellMargin;
          final padding = calendarStyle.cellPadding;
          final alignment = calendarStyle.cellAlignment;
          final duration = const Duration(milliseconds: 250);
          return AnimatedContainer(
            duration: duration,
            margin: margin,
            padding: padding,
            decoration: const BoxDecoration(
              color: ThemeColor.primaryColor,
              shape: BoxShape.circle,
            ),
            alignment: alignment,
            child: Text(
              text,
              style: calendarStyle.todayTextStyle,
            ),
          );
        },
      ),
    );
  }

  Widget _pointsInstruction() {
    TextStyle fontStyle = FontStyles.headerTextStyle_12.copyWith(
      color: ThemeColor.colorBlack40,
    );
    return Container(
      margin: EdgeInsets.only(top: 14),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '签到说明',
            style: fontStyle,
          ),
          Text(
            '1.每天0-24点均可签到，签到后获得积分，积分可在积分商城进行物品兑换',
            style: fontStyle,
          ),
          Text(
            '2.积分的获取和消耗可通过积分明细进行查看',
            style: fontStyle,
          ),
        ],
      ),
    );
  }

  Widget _signInCalendar() {
    return Container(
      padding: EdgeInsets.fromLTRB(15, 15, 15, 34),
      margin: EdgeInsets.symmetric(horizontal: 10, vertical: 14),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(12),
        color: ThemeColor.bkColorFFFFFF,
        boxShadow: [
          BoxShadow(
            color: Color(0xffE0E0E0).withOpacity(0.5),
            blurRadius: 5.0,
            offset: Offset(3.0, 0),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.only(bottom: 20),
            child: Text(
              '本月签到记录',
              style: FontStyles.headerTextStyle_22.copyWith(
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
          _tableCalendar(),
          SizedBox(
            height: 20,
          ),
          Divider(
            height: 1,
            color: ThemeColor.dividerColor_grey,
          ),
          _pointsInstruction(),
        ],
      ),
    );
  }
}
